﻿Imports System.Security.Cryptography
Imports System.Text
Imports System.Reflection
''' <summary>
''' check if the given user is in the database
''' </summary>
''' <remarks></remarks>
<GlobalAvalibleObjectImplementation(implementation_of:=GetType(IAuthetication), overrides_exsisting:=False, singleton:=True)>
Public Class DBAuthetication
    Implements IAuthetication

    Private Shared md5 As New MD5CryptoServiceProvider


    Public Function Hashstring(input As String) As String Implements IAuthetication.Hashstring
        Dim hash As Byte() = md5.ComputeHash(input.getBytes)
        Dim builder As New StringBuilder
        For Each b As Byte In hash
            builder.Append(b.ToString("x2"))
        Next
        Return builder.ToString
    End Function

    Public Function autheticate(username As String, password As String) As User Implements IAuthetication.autheticate
        Dim cd As IContentDispatcher = getGlobalObject(Of IContentDispatcher)()
        Dim pass As String = Hashstring(password)
        Dim filter As New User With {.username = New ContentPropertyFilter(Of String)(username), .password = New ContentPropertyFilter(Of String)(pass)}
        Return cd.getAllByFilter(Of User)(filter).FirstOrDefault
    End Function
End Class
